
function init() {
  // set up a MoniCA object
  var setupOptions = {
    updateInterval: 1000
  };
  // Set up to use the testing version of the JSON script.
  // setupOptions.webserverPath = 'cgi-bin/obstools/web_monica/' +
  //  'monicainterface_json_tester.pl';
  var narrabriMonica = monicaServer(setupOptions);
  var pointReferences = [];
  var tsReference;

  var updateTimeSeriesPage = function(pointRef) {
    var pDetails = pointRef.getPointDetails();
    var idPrefix = makeSafeId(pDetails.name);
    var pArray = pointRef.getTimeSeries();
    var nText = '';
    for (var i = 0; i < pArray.length; i++) {
      if (i > 0) {
				nText += ' , ';
      }
      nText += pArray[i][0] + ' == ' + pArray[i][1];
    }
    dojo.attr(idPrefix + '_Value', 'innerHTML', nText);
  };

  var updatePage = function(pointRef) {
    var pDetails = pointRef.getPointDetails();
    var idPrefix = makeSafeId(pDetails.name);
    var pState = pointRef.latestValue();
    var oState = pointRef.latestValue({
      valueAsDecimalDegrees: true });
    dojo.attr(idPrefix + '_Value', 'innerHTML',
	      pState.time + ' == ' + pState.value);
    dojo.attr(idPrefix + '_Options', 'innerHTML',
	      oState.time + ' == ' + oState.value);
  };

  var valuesGathered = function(serverInfo) {
    // fill in our web page
    for (var i = 0; i < pointReferences.length; i++) {
      var pDetails = pointReferences[i].getPointDetails();
      var idPrefix = makeSafeId(pDetails.name);
      var pState = pointReferences[i].latestValue();
      dojo.attr(idPrefix + '_Value', 'innerHTML', pState.value);
    }
  };

  var descriptionsGathered = function(serverInfo) {
    // fill in our web page
    for (var i = 0; i < pointReferences.length; i++) {
      var pDetails = pointReferences[i].getPointDetails();
      var idPrefix = makeSafeId(pDetails.name);
      dojo.attr(idPrefix + '_Name', 'innerHTML', pDetails.name);
      dojo.attr(idPrefix + '_Description', 'innerHTML', pDetails.description);
      dojo.attr(idPrefix + '_Units', 'innerHTML', pDetails.units);
    }

    var pDetails = tsReference.getPointDetails();
    var idPrefix = makeSafeId(pDetails.name);
    dojo.attr(idPrefix + '_Name', 'innerHTML', pDetails.name);
    dojo.attr(idPrefix + '_Description', 'innerHTML', pDetails.description);
    dojo.attr(idPrefix + '_Units', 'innerHTML', pDetails.units);

    // request that values be retrieved periodically
    narrabriMonica.startUpdating();
  };

  var makeSafeId = function(oName) {
    return oName.replace(/\./g, '_');
  };

  var addPointElements = function(pName) {
    // add some HTML DOM elements for the specified point
    var idPrefix = makeSafeId(pName);
    var nameEl = dojo.create('div', { id: idPrefix + '_Name' });
    dojo.byId('testarea').appendChild(nameEl);
    var descrEl = dojo.create('div', { id: idPrefix + '_Description' });
    dojo.byId('testarea').appendChild(descrEl);
    var valueEl = dojo.create('div', { id: idPrefix + '_Value' });
    dojo.byId('testarea').appendChild(valueEl);
    var unitsEl = dojo.create('div', { id: idPrefix + '_Units' });
    dojo.byId('testarea').appendChild(unitsEl);
    var optionsEl = dojo.create('div', { id: idPrefix + '_Options' });
    dojo.byId('testarea').appendChild(optionsEl);
  };

  var subscribed = function(serverInfo) {
    dojo.place('<span>Connected to MoniCA server ' +
	       serverInfo.server + '</span><br />',
	       'testarea');

    // add some points to the list
    var myPoints = ['capabxtemp.misc.cb.temps.Temp3',
		    'ca01.servo.coords.AzSkyPos'];
    pointReferences = narrabriMonica.addPoints(myPoints);
    for (var i = 0; i < myPoints.length; i++) {
      addPointElements(myPoints[i]);

      // add a callback for each point
      pointReferences[i].addCallback(updatePage);
    }

    // add a time-series point to the list
    var nTimeSeries = 'capabxtemp.misc.cb.temps.Temp4';
    addPointElements(nTimeSeries);
    tsReference = narrabriMonica.addTimeSeries({
      pointName: nTimeSeries,
      timeSeriesOptions: {
				startTime: -1, spanTime: 60, maxPoints: 60
      }
    });
    // add a callback for this point
    tsReference.addCallback(updateTimeSeriesPage);
    var tsO = tsReference.timeSeriesOptions();
    var tsOString = 'span: ' + tsO.spanTime + ', maxPoints: ' +
      tsO.maxPoints;
    var idPrefix = makeSafeId(nTimeSeries);
    dojo.attr(idPrefix + '_Options', 'innerHTML', tsOString);

    // request that the descriptions be retrieved
    narrabriMonica.getDescriptions();
  };

  // subscribe to a MoniCA connection
  dojo.subscribe('connection', subscribed);

  // subscribe to a MoniCA description channel
  dojo.subscribe('description', descriptionsGathered);

  // subscribe to a MoniCA value channel
  // dojo.subscribe('value', valuesGathered);

  // initialise the connection
  narrabriMonica.connect();
}

dojo.addOnLoad(init);